Skip to content

fix: AccumulateGrad stream mismatch warning when using DDP with Fabric & Trainer#21746

Merged
deependujha merged 10 commits into
masterfrom
fix/ddp-accumulate-grad-stream-mismatch-warning
Jun 1, 2026
Merged

fix: AccumulateGrad stream mismatch warning when using DDP with Fabric & Trainer#21746
deependujha merged 10 commits into
masterfrom
fix/ddp-accumulate-grad-stream-mismatch-warning

Conversation

@deependujha
Copy link
Copy Markdown
Collaborator

What does this PR do?

Fixes #21567

Problem

When using Fabric with DDP and gradient accumulation, the following warning
was emitted on every backward pass:

UserWarning: The AccumulateGrad node's stream does not match the stream of the
node that produced the incoming gradient. This may incur unnecessary
synchronization and break CUDA graph capture if the AccumulateGrad node's
stream is the default stream...

This warning did not appear when using plain PyTorch DDP directly.

Root Cause

The original setup_module initialized DistributedDataParallel inside a
new side-stream context (torch.cuda.Stream()):

ctx = torch.cuda.stream(torch.cuda.Stream())
with ctx:
    return DistributedDataParallel(...)

This was intentional for supporting CUDA graph whole-network capture, which
requires DDP to be initialized on a side-stream (see PyTorch docs:
https://docs.pytorch.org/docs/2.12/notes/cuda.html#id5).

However, for normal training (the vast majority of use cases), this causes a
stream mismatch: DDP registers its AccumulateGrad hooks on the side-stream
during initialization, but all subsequent forward/backward passes run on the
default stream. PyTorch detects this cross-stream node reference and emits
the warning.

Plain PyTorch DDP does not hit this because users initialize DDP directly
without any stream context, it defaults to the default stream, so there is
no mismatch.

Fix

Detect whether we are currently inside a CUDA graph capture context using
torch.cuda.is_current_stream_capturing(), and pick the appropriate stream:

  • Normal training (capturing=False): initialize DDP on the default
    stream. No mismatch, no warning.
  • CUDA graph capture (capturing=True): initialize DDP on a new
    side-stream as before (required by PyTorch). Additionally suppress the
    AccumulateGrad warning globally since the mismatch is intentional in
    this context.

This fix is applied to both DDPStrategy in lightning_fabric and
DDPStrategy in pytorch_lightning.

Testing

Verified with the reproduction script from #21567 across 4 GPUs (torch 2.11
and 2.12). Warning no longer appears under normal DDP + gradient accumulation
training.

Before submitting
  • Was this discussed/agreed via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or minor internal changes/refactors)

PR review

Anyone in the community is welcome to review the PR.
Before you start reviewing, make sure you have read the review guidelines. In short, see the following bullet-list:

Reviewer checklist
  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 28, 2026

⚡ Required checks status: All passing 🟢

Groups summary

🟢 pytorch_lightning: Tests workflow
Check ID Status
pl-cpu-guardian success

These checks are required after the changes to src/lightning/fabric/strategies/ddp.py, src/lightning/pytorch/strategies/ddp.py.

🟢 fabric: Docs
Check ID Status
docs-make (fabric, doctest) success
docs-make (fabric, html) success

These checks are required after the changes to src/lightning/fabric/strategies/ddp.py.

🟢 pytorch_lightning: Docs
Check ID Status
docs-make (pytorch, doctest) success
docs-make (pytorch, html) success

These checks are required after the changes to src/lightning/pytorch/strategies/ddp.py.

🟢 lightning_fabric: CPU workflow
Check ID Status
fabric-cpu-guardian success

These checks are required after the changes to src/lightning/fabric/strategies/ddp.py, tests/tests_fabric/strategies/test_ddp.py.

🟢 mypy
Check ID Status
mypy success

These checks are required after the changes to src/lightning/fabric/strategies/ddp.py, src/lightning/pytorch/strategies/ddp.py.

🟢 install
Check ID Status
install-pkg-guardian success

These checks are required after the changes to src/lightning/fabric/strategies/ddp.py, src/lightning/pytorch/strategies/ddp.py.


Thank you for your contribution! 💜

Note
This comment is automatically generated and updates for 70 minutes every 180 seconds. If you have any other questions, contact carmocca for help.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 28, 2026

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 50.00000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 87%. Comparing base (1120456) to head (5b7bb7c).
⚠️ Report is 1 commits behind head on master.
✅ All tests successful. No failed tests found.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #21746   +/-   ##
=======================================
- Coverage      87%      87%   -0%     
=======================================
  Files         270      270           
  Lines       23975    23987   +12     
=======================================
+ Hits        20750    20755    +5     
- Misses       3225     3232    +7     

@deependujha deependujha changed the title fix: AccumulateGrad stream mismatch warning when using DDP with Fabric fix: AccumulateGrad stream mismatch warning when using DDP with Fabric & Trainer Jun 1, 2026
Copy link
Copy Markdown
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice !

Comment thread src/lightning/pytorch/CHANGELOG.md Outdated
@deependujha deependujha enabled auto-merge (squash) June 1, 2026 12:30
@deependujha deependujha merged commit 35e56ef into master Jun 1, 2026
219 of 225 checks passed
@deependujha deependujha deleted the fix/ddp-accumulate-grad-stream-mismatch-warning branch June 1, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AccumulateGrad Warning

3 participants